package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.Activity;
import com.campsys.service.ActivityService;
import com.campsys.service.dto.ActivityDTO;
import com.campsys.service.dto.ActivityQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/activity")
@Slf4j
public class AdminActivityController {
    
    @Autowired
    private ActivityService activityService;
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("activity:view")
    @PostMapping("/list")
    public Result getActivityList(@RequestBody ActivityQueryDTO queryDTO) {
        Page<Activity> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return Result.ok(activityService.getActivityList(page, queryDTO));
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("activity:add")
    @PostMapping("/add")
    public Result addActivity(@RequestBody @Validated ActivityDTO activityDTO) {
        activityService.addActivity(activityDTO);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("activity:edit")
    @PostMapping("/update")
    public Result updateActivity(@RequestBody @Validated ActivityDTO activityDTO) {
        if (activityDTO.getId() == null) {
            return Result.error("活动ID不能为空");
        }
        activityService.updateActivity(activityDTO);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("activity:delete")
    @PostMapping("/delete/{id}")
    public Result deleteActivity(@PathVariable Long id) {
        activityService.deleteActivity(id);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("activity:edit")
    @PostMapping("/status/{id}")
    public Result updateActivityStatus(@PathVariable Long id, @RequestParam Integer status) {
        if (status != 0 && status != 1) {
            return Result.error("状态值只能是0或1");
        }
        activityService.updateStatus(id, status);
        return Result.ok();
    }
} 